package com.uniandes.edu.co.interfaz.tarea;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;

import com.uniandes.edu.co.dto.Ciclo;
import com.uniandes.edu.co.dto.Persona;
import com.uniandes.edu.co.dto.Tarea;
import com.uniandes.edu.co.dto.Tsp;
import com.uniandes.edu.co.excepciones.ExcepcionExiste;
import com.uniandes.edu.co.excepciones.ExcepcionParseInt;
import com.uniandes.edu.co.excepciones.ExcepcionVacio;
import com.uniandes.edu.co.interfaz.mundo.Principal;




/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
@SuppressWarnings("serial")
//public class InterfazModificarTarea extends javax.swing.JFrame {
public class InterfazModificarTarea extends JDialog {
	private Principal principal;
	private JButton jButton1;
	private JLabel jLabel5;
	private JComboBox jComboBox3;
	private JTextField jTextField4;
	private JLabel jLabel8;
	private JTextField jTextField3;
	private JLabel jLabel7;
	private JComboBox jComboBox2;
	private JLabel jLabel6;
	private JTextField jTextField2;
	private JComboBox jComboBox1;
	private JLabel jLabel4;
	private JTextArea jTextArea1;
	private JScrollPane jScrollPane1;
	private JLabel jLabel3;
	private JLabel jLabel2;
	private JButton jButton2;
	private JLabel jLabel1;

	/**
	* Auto-generated main method to display this JFrame
	*/
	/*public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				InterfazAgregarTarea inst = new InterfazAgregarTarea();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public InterfazAgregarTarea() {
		super();
		initGUI();
	}*/
	
	public InterfazModificarTarea(Principal id) {
		super(id, true);
	    principal = id;
	    setTitle( "Modificar Tarea" );
	    initGUI();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			{
				jButton1 = new JButton();
				getContentPane().add(jButton1);
				jButton1.setText("Regresar");
				jButton1.setBounds(222, 371, 130, 23);
				jButton1.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						dispose();
					}
				});
			}
			{
				jLabel1 = new JLabel();
				getContentPane().add(jLabel1);
				jLabel1.setText("Modificar Tarea");
				jLabel1.setBounds(46, 18, 327, 29);
				jLabel1.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
				jLabel1.setFont(new java.awt.Font("Segoe UI",1,14));
				jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
				jLabel1.setHorizontalTextPosition(SwingConstants.CENTER);
			}
			{
				jButton2 = new JButton();
				getContentPane().add(jButton2);
				jButton2.setText("Modificar");
				jButton2.setBounds(46, 371, 130, 23);
				jButton2.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						String idPersonaSeleccionada=(String)jComboBox1.getSelectedItem();
						Integer idCicloSeleccionado=(Integer)jComboBox2.getSelectedItem();
						String idTareaSeleccionada=(String)jComboBox3.getSelectedItem();
						try{
							if(idPersonaSeleccionada.equalsIgnoreCase("Seleccione...")){
								JOptionPane.showMessageDialog(null, "Debe seleccionar la persona encargada de la tarea");
							}else{
								if(idCicloSeleccionado==null || idCicloSeleccionado==0){
									JOptionPane.showMessageDialog(null, "Debe seleccionar el ciclo al cual pertenece la tarea");
								}else{
									try{
										Persona persona=Tsp.getSingleton().consultarPersonaNombre(idPersonaSeleccionada, Tsp.getSingleton().getPersonas());
										Ciclo ciclo=Tsp.getSingleton().obtenerCiclo(idCicloSeleccionado.toString());
										Tsp.getSingleton().modificarTarea(idTareaSeleccionada, jTextArea1.getText(), jTextField2.getText(), jTextField3.getText(), jTextField4.getText(), persona.getIdPersona(), ciclo.getTareas());
										JOptionPane.showMessageDialog(null, "Se ha modificado la tarea correctamente");
										dispose();
									}catch (ExcepcionVacio e) {
										// TODO Auto-generated catch block
										JOptionPane.showMessageDialog(null, e.getMessage());
									}catch (ExcepcionExiste e) {
										// TODO Auto-generated catch block
										JOptionPane.showMessageDialog(null, e.getMessage());
									}catch (ExcepcionParseInt e) {
										// TODO Auto-generated catch block
										JOptionPane.showMessageDialog(null, e.getMessage());
									}
								}
							}
						}catch(Exception e){
							JOptionPane.showMessageDialog(null, "La seleccion de la tarea no es valida");
						}
					}
				});
			}
			{
				jLabel2 = new JLabel();
				getContentPane().add(jLabel2);
				jLabel2.setText("id Tarea");
				jLabel2.setBounds(12, 97, 105, 19);
				jLabel2.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
			}
			{
				jLabel3 = new JLabel();
				getContentPane().add(jLabel3);
				jLabel3.setText("Descripcion");
				jLabel3.setBounds(12, 130, 105, 19);
				jLabel3.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
			}
			{
				jScrollPane1 = new JScrollPane();
				getContentPane().add(jScrollPane1);
				jScrollPane1.setBounds(152, 130, 247, 80);
				{
					jTextArea1 = new JTextArea();
					jScrollPane1.setViewportView(jTextArea1);
					jTextArea1.setPreferredSize(new java.awt.Dimension(241, 77));
				}
			}
			{
				jLabel4 = new JLabel();
				getContentPane().add(jLabel4);
				jLabel4.setText("Tiempo Estimado");
				jLabel4.setBounds(12, 221, 105, 19);
				jLabel4.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
			}
			{
				jLabel5 = new JLabel();
				getContentPane().add(jLabel5);
				jLabel5.setText("responsable");
				jLabel5.setBounds(12, 329, 105, 19);
				jLabel5.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
			}
			{
				jTextField2 = new JTextField();
				getContentPane().add(jTextField2);
				jTextField2.setBounds(152, 219, 246, 23);
			}
			{
				jLabel6 = new JLabel();
				getContentPane().add(jLabel6);
				jLabel6.setText("Ciclo");
				jLabel6.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
				jLabel6.setBounds(12, 67, 105, 21);
			}
			{
				List <Persona> personas=Tsp.getSingleton().getPersonas();
				List <String> nomPersonas=new ArrayList<String>();
				nomPersonas.add("Seleccione...");
				for (Persona per : personas) {
					nomPersonas.add(per.getNombre());
				}
				ComboBoxModel jComboBox1Model = 
					new DefaultComboBoxModel(
							nomPersonas.toArray());
				jComboBox1 = new JComboBox();
				getContentPane().add(jComboBox1);
				jComboBox1.setModel(jComboBox1Model);
				jComboBox1.setBounds(152, 327, 246, 23);
			}
			{
				List <Ciclo> ciclos=Tsp.getSingleton().getCiclos();
				List <Integer> idCiclos=new ArrayList<Integer>();
				for (Ciclo cic : ciclos) {
					idCiclos.add(cic.getIdCiclo());
				}
				ComboBoxModel jComboBox2Model = 
					new DefaultComboBoxModel(
							idCiclos.toArray());
				jComboBox2 = new JComboBox();
				getContentPane().add(jComboBox2);
				jComboBox2.setModel(jComboBox2Model);
				jComboBox2.setBounds(152, 65, 246, 23);
				jComboBox2.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						llenarTareas();
					}
				});
			}
			{
				jLabel7 = new JLabel();
				getContentPane().add(jLabel7);
				jLabel7.setText("Valor Ganado Estimado");
				jLabel7.setBounds(12, 252, 140, 16);
			}
			{
				jTextField3 = new JTextField();
				getContentPane().add(jTextField3);
				jTextField3.setBounds(152, 249, 246, 23);
			}
			{
				jLabel8 = new JLabel();
				getContentPane().add(jLabel8);
				jLabel8.setText("Locs Estimados");
				jLabel8.setBounds(12, 289, 122, 16);
			}
			{
				jTextField4 = new JTextField();
				getContentPane().add(jTextField4);
				jTextField4.setBounds(152, 286, 246, 23);
			}
			{
				List <Ciclo> ciclos=Tsp.getSingleton().getCiclos();
				Integer idCicloSeleccionado=(Integer)jComboBox2.getSelectedItem();
				List <String> idTarea=new ArrayList<String>();
				for (Ciclo cic : ciclos) {
					if(cic.getIdCiclo()==idCicloSeleccionado){
						List<Tarea> listaTareas=cic.getTareas();
						for (Tarea tar : listaTareas) {
							idTarea.add(tar.getIdTarea());
						}
					}
				}
				ComboBoxModel jComboBox3Model = 
					new DefaultComboBoxModel(
							idTarea.toArray());
				jComboBox3 = new JComboBox();
				getContentPane().add(jComboBox3);
				jComboBox3.setModel(jComboBox3Model);
				jComboBox3.setBounds(152, 95, 246, 23);
				
				try {
					String idTareaSeleccionado=(String)jComboBox3.getSelectedItem();
					for (Ciclo cic : ciclos) {
						if(cic.getIdCiclo()==idCicloSeleccionado){
							List<Tarea> listaTareas=cic.getTareas();
							Tarea tar=Tsp.getSingleton().consultarTarea(idTareaSeleccionado, listaTareas);
							jTextArea1.setText(tar.getDescripcion());
							jTextField2.setText(""+tar.getTiempoEstimado());
							jTextField3.setText(""+tar.getValorGanadoEstimado());
							jTextField4.setText(""+tar.getLocsEstimado());
							Persona persona=Tsp.getSingleton().consultarPersona(tar.getIdPersona(), Tsp.getSingleton().getPersonas());
							jComboBox1.setSelectedItem(persona.getNombre());
						}
					}
				} catch (Exception e) {
					// TODO: handle exception
				}
				
				jComboBox3.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						try {
							String idTareaSeleccionado=(String)jComboBox3.getSelectedItem();
							List <Ciclo> ciclos=Tsp.getSingleton().getCiclos();
							Integer idCicloSeleccionado=(Integer)jComboBox2.getSelectedItem();
							List <String> idTarea=new ArrayList<String>();
							for (Ciclo cic : ciclos) {
								if(cic.getIdCiclo()==idCicloSeleccionado){
									List<Tarea> listaTareas=cic.getTareas();
									Tarea tar=Tsp.getSingleton().consultarTarea(idTareaSeleccionado, listaTareas);
									jTextArea1.setText(tar.getDescripcion());
									jTextField2.setText(""+tar.getTiempoEstimado());
									jTextField3.setText(""+tar.getValorGanadoEstimado());
									jTextField4.setText(""+tar.getLocsEstimado());
									Persona persona=Tsp.getSingleton().consultarPersona(tar.getIdPersona(), Tsp.getSingleton().getPersonas());
									jComboBox1.setSelectedItem(persona.getNombre());
								}
							}
						} catch (Exception e) {
							// TODO: handle exception
						}
					}
				});
			}
			pack();
			this.setSize(446, 456);
			centrarVentana();
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}
	
    private void centrarVentana(){
        Dimension dPantalla = Toolkit.getDefaultToolkit( ).getScreenSize( );
        Dimension dVentana = getSize( );

        int xEsquina = ( dPantalla.width / 2 ) - ( dVentana.width / 2 );
        int yEsquina = ( dPantalla.height / 2 ) - ( dVentana.height / 2 );

        setLocation( xEsquina, yEsquina );
    }
    
    private void llenarTareas(){
    	List <Ciclo> ciclos=Tsp.getSingleton().getCiclos();
		Integer idCicloSeleccionado=(Integer)jComboBox2.getSelectedItem();
		List <String> idTarea=new ArrayList<String>();
		for (Ciclo cic : ciclos) {
			if(cic.getIdCiclo()==idCicloSeleccionado){
				List<Tarea> listaTareas=cic.getTareas();
				for (Tarea tar : listaTareas) {
					idTarea.add(tar.getIdTarea());
				}
			}
		}
		ComboBoxModel jComboBox3Model = 
			new DefaultComboBoxModel(
					idTarea.toArray());
		jComboBox3.setModel(jComboBox3Model);
		
		try {
			String idTareaSeleccionado=(String)jComboBox3.getSelectedItem();
			for (Ciclo cic : ciclos) {
				if(cic.getIdCiclo()==idCicloSeleccionado){
					List<Tarea> listaTareas=cic.getTareas();
					Tarea tar=Tsp.getSingleton().consultarTarea(idTareaSeleccionado, listaTareas);
					jTextArea1.setText(tar.getDescripcion());
					jTextField2.setText(""+tar.getTiempoEstimado());
					jTextField3.setText(""+tar.getValorGanadoEstimado());
					jTextField4.setText(""+tar.getLocsEstimado());
					Persona persona=Tsp.getSingleton().consultarPersona(tar.getIdPersona(), Tsp.getSingleton().getPersonas());
					jComboBox1.setSelectedItem(persona.getNombre());
				}
			}
		} catch (ExcepcionVacio e) {
			jTextArea1.setText(null);
			jTextField2.setText(null);
			jTextField3.setText(null);
			jTextField4.setText(null);
			jComboBox1.setSelectedItem(null);
		}
    }

}

